package com.eco.controller;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.fastjson.JSONObject;
import com.eco.constants.Constants;
import com.eco.entity.User;
import com.eco.service.UserService;
import com.eco.utils.GenerateToken;
import com.eco.utils.MD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.tags.BindTag;

@RestController
/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/controller/UserController.class */
public class UserController {
    private static final Logger log = LoggerFactory.getLogger((Class<?>) UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private GenerateToken generateToken;

    @PostMapping({"/addUser"})
    public JSONObject addUser(String usercode, String password) {
        JSONObject result = new JSONObject();
        if (StringUtils.isEmpty(usercode) || StringUtils.isEmpty(password)) {
            log.error("添加用户失败，输入的参数为值");
            result.put("msg", (Object) "有参数为空");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        User selectUser = this.userService.findByUsername(usercode);
        if (selectUser != null) {
            log.error("添加用户失败，该用户已存在");
            result.put("msg", (Object) "用户已存在");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        String dbPassword = MD5Utils.generate(password);
        User user = new User(0, usercode, null, dbPassword, Constants.MD5_SALT, null, null, null);
        int isok = this.userService.addByUser(user);
        if (isok < 1) {
            log.error("添加用户失败，操作数据库异常");
            result.put("msg", (Object) "操作数据库异常");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        log.info("添加用户成功");
        result.put("msg", (Object) "添加用户成功");
        result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
        result.put("data", (Object) null);
        return result;
    }

    @PutMapping({"/updateUser"})
    public JSONObject updateUser(String usercode, String password) {
        JSONObject result = new JSONObject();
        if (StringUtils.isEmpty(usercode) || StringUtils.isEmpty(password)) {
            log.error("修改失败，输入的参数为值");
            result.put("msg", (Object) "有参数为空");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        User selectUser = this.userService.findByUsername(usercode);
        if (selectUser == null) {
            log.error("修改失败，该用户不存在");
            result.put("msg", (Object) "该用户不存在");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        String dbPassword = MD5Utils.generate(password);
        selectUser.setPassword(dbPassword);
        int isok = this.userService.updateByUser(selectUser);
        if (isok < 1) {
            log.error("修改失败，操作数据库异常");
            result.put("msg", (Object) "操作数据库异常");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        log.info("修改成功");
        result.put("msg", (Object) "修改成功");
        result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
        result.put("data", (Object) null);
        return result;
    }

    @PostMapping(value = {"/login"}, consumes = {"application/json"})
    public JSONObject login(@RequestBody JSONObject data) {
        String usercode = data.getString("usercode");
        String password = data.getString(DruidDataSourceFactory.PROP_PASSWORD);
        JSONObject result = new JSONObject();
        if (StringUtils.isEmpty(usercode) || StringUtils.isEmpty(password)) {
            log.error("登录失败，输入的参数为空");
            result.put("msg", (Object) "有参数为空");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        String outPassword = MD5Utils.generate(password);
        User selectUser = this.userService.findByUsernameAndPassword(usercode, outPassword);
        if (selectUser == null) {
            log.error("登录失败，用户名或密码错误");
            result.put("msg", (Object) "用户名或密码错误");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        String token = this.generateToken.createToken(Constants.USER_TOKEN_PREFIX, selectUser.getUserId().toString());
        log.info("登陆成功,生成token:[{}]", token);
        result.put("msg", (Object) "登陆成功");
        result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
        result.put("token", (Object) token);
        return result;
    }
}